import { timeFormat } from '../../model/utils/common';
import {mlist} from './maintenance';

export default {
    namespace: 'maintenance',
    state: {
        carNumber: '',
        carList: [],
        list: [],
        mainTenList: []
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        * getCarList({ payload }, { put, call, select }){
        // 查询汽车列表
            const carList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });

            let price = {};
            if(carList && carList.length){
                const carNumber = wx.getStorageSync('carNumber');
                let car = carList.find(item => item.carNumber == carNumber) || carList[0];
                wx.setStorageSync('carNumber', car.carNumber);
                yield put({
                    type : 'save',
                    payload : {
                        carList,
                        price,
                        carNumber: car.carNumber
                    }
                });
                // 第一次初始化
                if(!payload.carNumber){
                    yield put({
                        type: 'init',
                        payload: {
                            carNumber : car.carNumber
                        }
                    });
                }
            }else{
                yield put({
                    type: 'init',
                    payload: {
                        carNumber : ''
                    }
                });
            }
            return carList;
        },
        *init({ payload }, { put, call, select }){

            const {carNumber} = payload;
            const list = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/maintenance/list',
                    data: {carNumber}
                }
            });
            list.forEach(item => {
                item.timeStr = timeFormat(item.time, 'YYYY-MM-DD hh:mm:ss');
            });
            yield put({
                type: 'save',
                payload: {
                    list,
                    carNumber
                }
            });

            yield put({
                type: 'getMaintenItem',
                payload: {
                    list,
                    carNumber
                }
            });

        },
        /**
         * 计算每个项目的保养记录
         * @param {*} param0 
         * @param {*} param1 
         */
        * getMaintenItem({ payload }, { put, call, select }){
            const {list,carNumber} = payload;
            let lastOilData = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/getLatData',
                    data: {carNumber}
                }
            });
            console.log('最后一条加油记录',lastOilData);
            lastOilData = lastOilData ||{mileage: 0};
            
            let mainTenanceMap = {};
            const current = Date.now();
            // 循环保养项目
            mlist.forEach(item => {
                console.log(item.type);
                // 如果没有初始化项目
                if(!mainTenanceMap[item.type]){
                    const it = list.find(i => i[item.type]);
                    let lastMain = list[0]||{};
                    const durationTime =  it ?item.time - ((current - it.time)/86400000).toFixed(1) : '--';
                    const durationMileage = it ?item.mileage - (Math.max(lastMain.mileage,lastOilData.mileage) - it.mileage) : '--';
                    mainTenanceMap[item.type] = {
                        base: it,
                        durationTime,
                        durationMileage,
                        info: item
                    };
                }
            });
            const mainTenList = Object.values(mainTenanceMap);
            mainTenList.sort((a,b) => b.durationTime > a.durationTime ? 1: -1);
            yield put({
                type: 'save',
                payload: {
                    mainTenList
                }
            });
            
            console.log('mainTenanceMap',mainTenanceMap);
        }
    }
};